package com.weimob.mcloud.wechatwork.customer.job;

import com.alibaba.fastjson.JSON;
import com.weimob.elasticjob.annotation.ElasticJob;
import com.weimob.elasticjob.executor.context.ExecutorContext;
import com.weimob.mcloud.wechatwork.customer.redis.RedisService;
import com.weimob.mcloud.wechatwork.customer.service.user.module.req.dynamic.DynamicThreadPollReq;
import com.weimob.mcloud.wechatwork.customer.service.user.service.DynamicThreadPoolService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author yuanhao
 * @date 2023/1/12 14:27
 */
@Slf4j
@Component
public class DynamicThreadPoolJob {

    @Resource
    RedisTemplate<String, String> redisTemplate;

    @Autowired
    private RedisService redisService;

    @Resource
    private DynamicThreadPoolService dynamicThreadPoolService;

    @ElasticJob(name = "dynamicThreadPoolJob")
    public void dynamicThreadPoolJob(ExecutorContext executorContext) {
        Map<String, String> map = executorContext.getTriggerCustomParameter();
        log.info("dynamicThreadPoolJob.triggerCustomParameter:{}", JSON.toJSONString(map));
        String threadPoolName = map.get("threadPoolName");
        if (StringUtils.isBlank(threadPoolName)) {
            log.error("修改线程池入参校验失败，threadPoolName 为空");
            return;
        }
        Integer coreSize = Objects.nonNull(map.get("corePoolSize")) ? Integer.valueOf(map.get("corePoolSize")) : null;
        Integer maxCoreSize = Objects.nonNull(map.get("maxPoolSize")) ? Integer.valueOf(map.get("maxPoolSize")) : null;
        Integer queueSize = Objects.nonNull(map.get("queueSize")) ? Integer.valueOf(map.get("queueSize")) : null;
        Boolean showThreadPoolMetadata = Objects.nonNull(map.get("showThreadPoolMetadata")) ? Boolean.valueOf(map.get("showThreadPoolMetadata")) : Boolean.FALSE;

        DynamicThreadPollReq dynamicThreadPollReq = new DynamicThreadPollReq();
        dynamicThreadPollReq.setThreadPoolName(threadPoolName);
        dynamicThreadPollReq.setCorePoolSize(coreSize);
        dynamicThreadPollReq.setMaxPoolSize(maxCoreSize);
        dynamicThreadPollReq.setQueueSize(queueSize);
        dynamicThreadPollReq.setShowThreadPoolMetadata(showThreadPoolMetadata);

        dynamicThreadPoolService.dynamicCoreAndMaximumPoolSize(dynamicThreadPollReq);

    }

    @ElasticJob(name = "delRedisJob")
    public void delRedisKey(ExecutorContext executorContext) {
        Map<String, String> map = executorContext.getTriggerCustomParameter();
        //指定key删除
        String key = map.get("key");
        if (StringUtils.isNotBlank(key)) {
            log.info("指定redisKey：{} 删除", key);
            Boolean hasKey = redisTemplate.hasKey(key);
            if (Boolean.FALSE.equals(hasKey)) {
                log.error("当前redisKey:{} 不存在", key);
                return;
            }
            Boolean delete = redisTemplate.delete(key);
            log.info("当前redisKey:{},删除结果:{}", key, delete);
        }

        //模糊删除
        String prefixKey = map.get("prefixKey");
        if (StringUtils.isNotBlank(prefixKey)) {
            log.info("指定prefixKey：{} 删除", prefixKey);
            List<String> list = redisService.scan(redisTemplate, prefixKey + "*");
            Long removeCount = redisService.remove(list);
            log.info("根据prefixKey:{} 删除，执行完毕,总条数:{},成功条数:{}", prefixKey, list.size(), removeCount);
        }

    }


}
